package com.teangtang.fly.service;

import com.teangtang.fly.dto.OrderDTO;
import com.teangtang.fly.dto.TicketDTO;
import com.teangtang.fly.entity.MyOrder;
import com.teangtang.fly.entity.Ticket;
import com.teangtang.fly.entity.Traveller;
import com.teangtang.fly.repository.MyOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MyOrderService {
    @Autowired
    private MyOrderRepository myOrderRepository;

    @Autowired
    TicketService ticketService;

    @Autowired
    TravellerService travellerService;

    public List<OrderDTO> getAllOrders() {
        List<MyOrder> myOrders = myOrderRepository.findAll();
        List<OrderDTO> orderDTOS = new ArrayList<>();
        for(MyOrder myOrder : myOrders) {
            OrderDTO orderDTO = new OrderDTO();
            orderDTO.setOrderId(myOrder.getOrderId());
            orderDTO.setIdCard(myOrder.getTraveller().getIdCard());
            orderDTO.setIdCardType(myOrder.getTraveller().getIdCardType());
            orderDTO.setTotalPrice(myOrder.getTotalPrice());
            orderDTO.setStatus(myOrder.getStatus());
            orderDTO.setCreateTime(myOrder.getCreateTime());
            List<Ticket> tickets = myOrder.getTickets();
            List<TicketDTO> ticketDTOS = new ArrayList<>();
            for (Ticket ticket : tickets) {
                TicketDTO ticketDTO = new TicketDTO();
                ticketDTO.setId(ticket.getId());
                ticketDTO.setSeatNumber(ticket.getSeatNumber());
                ticketDTO.setFlightId(ticket.getFlight().getFlightId());
                ticketDTO.setIdCard(ticket.getTraveller().getIdCard());
                ticketDTO.setStatus(ticket.getStatus());
                ticketDTOS.add(ticketDTO);
            }
            orderDTO.setTickets(ticketDTOS);
            orderDTO.setCreateTime(myOrder.getCreateTime());
            orderDTOS.add(orderDTO);
        }
        return orderDTOS;
    }
    public boolean addOrder(OrderDTO orderDTO) {
        Traveller traveller = travellerService.getTravellerById(orderDTO.getIdCard());
        List<Ticket> tickets = new ArrayList<>();
        MyOrder myOrder = new MyOrder();
        Long totalPrice = 0L;
        for(TicketDTO ticketDTO : orderDTO.getTickets()){
            Traveller ticketTraveller = travellerService.getTravellerById(ticketDTO.getIdCard());
            Ticket ticket = ticketService.getTicketById(ticketDTO.getId());
            if(ticketDTO.getStatus().equals("0")){
                ticket.setTraveller(null);
                ticket.setStatus("0");
                ticket.setMyOrder(null);
                ticketService.createTicket(ticket);
                continue;
            }
            ticket.setTraveller(ticketTraveller);
            ticket.setStatus(ticketDTO.getStatus());
            ticket.setMyOrder(myOrder);
            totalPrice += ticket.getPrice();
            tickets.add(ticket);
        }
        myOrder.setTraveller(traveller);
        myOrder.setStatus(orderDTO.getStatus());
        myOrder.setTotalPrice(totalPrice);
        myOrder.setTickets(tickets);
        myOrder.setCreateTime(new Date());//获取当前时间
        myOrderRepository.save(myOrder);
        return true;

    }

}
